import axios from "axios";
import { BASE_URL } from "../config/http.js";

export function getHomeData() {
  return axios
    .get(`${BASE_URL}/api/homeData/`)
    .then((resp) => {
      if (resp.data) {
        return resp.data;
      } else {
        throw resp.data;
      }
    })
    .catch((err) => {
      if (err.response && err.response.data) {
        throw err.response.data;
      } else {
        throw { error: "网络错误" };
      }
    });
}

export function getBookData() {
  return axios
    .get(`${BASE_URL}/api/bookData/`)
    .then((resp) => {
      if (resp.data) {
        return resp.data;
      } else {
        throw resp.data;
      }
    })
    .catch((err) => {
      if (err.response && err.response.data) {
        throw err.response.data;
      } else {
        throw { error: "网络错误" };
      }
    });
}

export function getCharactersData() {
  return axios
    .get(`${BASE_URL}/api/charactersData`)
    .then((resp) => {
      if (resp.data) {
        return resp.data;
      } else {
        throw resp.data;
      }
    })
    .catch((err) => {
      if (err.response && err.response.data) {
        throw err.response.data;
      } else {
        throw { error: "网络错误" };
      }
    });
}

export function getPageAssets() {
  return axios.get(`${BASE_URL}/api/pageAssets`).then((res) => res.data);
}

export function getVideoData() {
  return axios.get(`${BASE_URL}/api/videoData`).then((res) => res.data);
}

export function getAnnouncementData() {
  return axios
    .get(`${BASE_URL}/api/announcementData`)
    .then((resp) => resp.data)
    .catch((err) => {
      if (err.response && err.response.data) {
        throw err.response.data;
      } else {
        throw { error: "网络错误" };
      }
    });
}
